package com.ykdl.member.kid.regandlogin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import net.wxxr.dataparse.model.DataParseCode;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.util.Constants;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.RegisterPhonebean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.util.ResoureUtil;
import com.ykdl.member.kid.util.StringUtils;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class PhoneNumberRegisterTwoActivity extends BaseScreen {
	private EditText user_name,user_pw ;
	private RelativeLayout edit_pw_lp ;
	String phonesString = "" ;
	String verifyCode = "" ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("注册", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		}, R.string.turn_ok, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				gonext();
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.qqandsinaregisteractivity_xml);
		ResoureUtil.activityList.add(this);
		phonesString = this.getIntent().getStringExtra("phone_number");
		verifyCode = this.getIntent().getStringExtra("verifyCode");
		user_name = (EditText)findViewById(R.id.user_name);
		user_pw = (EditText)findViewById(R.id.user_pw);
		edit_pw_lp = (RelativeLayout)findViewById(R.id.edit_pw_lp); 
	}
	public void gonext() {
		String name = user_name.getText().toString().trim();
		String password=user_pw.getText().toString().trim();
		/*if (user_name.getText().toString().trim().length()>1&& user_pw.getText().toString().trim().length()>1) {
			getToken ();
		}else {
			Toast.makeText(mContext, "请输入用户名密码", 1).show();
		}*/
		if(StringUtils.chineseLength(name)<3 ||StringUtils.chineseLength(name)>24){
			Toast.makeText(this, "用户昵称应在3-24个字符", 1).show();
			return;
		}
		if(StringUtils.chineseLength(password)<6 || StringUtils.chineseLength(password)>20){
			Toast.makeText(this, "用户密码应在6-20个字符", 1).show();
			return;
		}
		getToken ();
	}
	public void getToken () {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.REGISTER_PHONE;
		HttpParameters bodyParams = new HttpParameters();
		bodyParams.put("client_id", Constants.CLIENT_ID);
		bodyParams.put("client_secret", Constants.CLIENT_SECRET);
		String username="";
		try {
			 username = URLEncoder.encode(user_name.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bodyParams.put("display_name", username);
//		URLEncoder.encode(placeString, "utf8")
		bodyParams.put("phone", phonesString);
		bodyParams.put("password", user_pw.getText().toString());
		bodyParams.put("confirm_code", verifyCode);
		bodyParams.put("app_key", "hsh");
		
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,bodyParams, null);
		 TaskManager.startHttpRequest(request, new resetpasswordTag(),  RegisterPhonebean.class);
		
	}
	class resetpasswordTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof RegisterPhonebean) {
				
				RegisterPhonebean bean = (RegisterPhonebean) t;
				if (bean.getError() == null) {
					Toast.makeText(mContext, "登录成功", 1).show();
					AccessTokenKeeper.keepAccessToken(PhoneNumberRegisterTwoActivity.this, bean);
						Intent intent = new Intent(PhoneNumberRegisterTwoActivity.this,StateSelectActivity.class);
						intent.putExtra("isShow", true);
						PhoneNumberRegisterTwoActivity.this.startActivity(intent);
						PhoneNumberRegisterTwoActivity.this.finish();
				}else {
					Toast.makeText(mContext, bean.getDesc(), 1).show();
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			String errMsg = parseError.getErrMsg();
			DataParseCode errorCodeString = parseError.getErrorCode();
			finishProgress();
			Toast.makeText(mContext, "参数有误失败", 1).show();
			return true;
			
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
}
